<?php

namespace app\common\logic;

class Queue
{

    /**
     * 添加会员积分
     * @param unknown $member_info
     */
    public function addPoint($member_info)
    {
        $points_model = model('Common/Points');
        $points_model->savePointsLog('login', array('pl_memberid' => $member_info['id'], 'pl_membername' => $member_info['name']), true);
        return callback(true);
    }

    /**
     * 添加会员经验值
     * @param unknown $member_info
     */
    public function addExppoint($member_info)
    {
        $exppoints_model = model('Common/Exppoints');
        $exppoints_model->saveExppointsLog('login', array('exp_memberid' => $member_info['id'], 'exp_membername' => $member_info['name']), true);
        return callback(true);
    }

    /**
     * 下单变更库存销量
     * @param unknown $goods_buy_quantity
     */
    public function createOrderUpdateStorage($goods_buy_quantity)
    {
        foreach ($goods_buy_quantity as $goods_id => $quantity) {
            $data = array();
            $data['goods_storage'] = array('exp', 'goods_storage-' . $quantity);
            $data['goods_salenum'] = array('exp', 'goods_salenum+' . $quantity);
            $result = rpc('common/common/update', ["goods", ['id' => $goods_id], $data]);
            //变更店铺销量
            $store_info = rpc('common/common/one', ['goods', ['id' => $goods_id], ['field' => 'store_id']]);
            $store_id = intval($store_info['store_id']);
            if ($store_id > 0) {
                $data1 = [];
                $data1['store_sales'] = array('exp', 'store_sales+' . $quantity);
                $result = rpc('common/common/update', ["store", ['id' => $store_id], $data1]);
            }
        }
        if (!$result) {
            return callback(false, '变更商品库存与销量失败');
        } else {
            return callback(true);
        }
    }

    /**
     * 取消订单变更库存销量
     * @param unknown $goods_buy_quantity
     */
    public function cancelOrderUpdateStorage($goods_buy_quantity)
    {
        $model_goods = model('Common/Goods');
        $model_store = model('Common/Store');
        foreach ($goods_buy_quantity as $goods_id => $quantity) {
            $data = array();
            $data['goods_storage'] = array('exp', 'goods_storage+' . $quantity);
            $data['goods_salenum'] = array('exp', 'goods_salenum-' . $quantity);
            $result = $model_goods->editGoodsById($data, $goods_id);
            //变更店铺销量
            $store_info = $model_goods->getGoodsInfo(array('id' => $goods_id), 'store_id');
            $store_id = intval($store_info['store_id']);
            if ($store_id > 0) {
                $data1['store_sales'] = array('exp', 'store_sales-' . $quantity);
                $result = $model_store->editStore($data1, array('id' => $store_id));
            }
        }
        if (!$result) {
            return callback(false, '变更商品库存与销量失败');
        } else {
            return callback(true);
        }
    }

    /**
     * 删除购物车
     * @param unknown $cart
     */
    public function delCart($cart)
    {
        if (!is_array($cart['cart_ids']) || empty($cart['buyer_id'])) {
            return callback(true);
        }

        $del = rpc('common/common/delete', ['cart', ['buyer_id' => $cart['buyer_id'], 'cart_id' => array('in', $cart['cart_ids'])]]);
        if (!$del) {
            return callback(false, '删除购物车数据失败');
        } else {
            return callback(true);
        }
    }

    /**
     * 发送店铺消息
     */
    public function sendStoreMsg($param)
    {
        $send = new \SendStoreMsg();
        $send->set('code', $param['code']);
        $send->set('store_id', $param['store_id']);
        $send->send($param['param']);
        return callback(true);
    }

    /**
     * 发送会员消息
     */
    public function sendMemberMsg($param)
    {
        $send = new \SendMemberMsg();
        $send->set('code', $param['code']);
        $send->set('member_id', $param['member_id']);
        if (!empty($param['number']['mobile'])) {
            $send->set('mobile', $param['number']['mobile']);
        }
        if (!empty($param['number']['email'])) {
            $send->set('email', $param['number']['email']);
        }
        
        $send->send($param['param']);
        return callback(true);
    }

}
